import { useEffect, useState } from "react"

export const isFalsy = (val:any) => val === 0 ? false : !val

export const cleanObject = (object:object) => {
  const res = { ...object }
  Object.keys(res).forEach(key => {
    // @ts-ignore
    const value = res[key]
    if(isFalsy(value)){
      // @ts-ignore
      delete res[key]
    }
  })
  return res
}

export const useMount = (callback: () => void) => {
  useEffect(()=>{
    callback()
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
}

export const useDebounce = <V>(value: V, delay?: number) => {
  const [debouncedValue, setDebouncedValue] = useState(value)
  useEffect(() => {
    const timeout  = setTimeout(() => setDebouncedValue(value),delay)
    return () => clearTimeout(timeout)
  }, [value, delay])
  return debouncedValue
}